package com.zlzlib.libuimodel.ui;

import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.Observer;

import com.zlzlib.librx.TUtil;
import com.zlzlib.libui.activity.BaseActivity;
import com.zlzlib.libuimodel.bean.SourceData;
import com.zlzlib.libuimodel.model.BaseViewModel;
import com.zlzlib.libuimodel.model.RxDataModel;


/**
 * Created by zlz on 2019/9/2.
 */
public abstract class BaseModelActivity<T extends BaseViewModel<? extends RxDataModel>,
        B extends ViewDataBinding> extends BaseActivity<B> {

    protected T mViewModel;

    protected abstract void pageData(SourceData source);

    protected abstract void pageLoading(SourceData source);

    protected abstract void pageTip(SourceData source);

    @Override
    protected void initView() {
        super.initView();
        mViewModel = (T) initViewMode((Class<T>) TUtil.getInstance(this, 0));
    }

    /**
     * 初始化ViewMode
     *
     * @param tClass 继承 BaseViewModel 的类
     */
    protected <M extends BaseViewModel<? extends RxDataModel>> M initViewMode(Class<M> tClass) {
        M baseViewModel = Utils.getBaseViewModel(this, tClass);
        baseViewModel.addPageDataObserve(this, getClass().getSimpleName(), observerData);
        baseViewModel.addPageLoadingObserve(this, getClass().getSimpleName(), observerLoading);
        baseViewModel.addPageTipObserve(this, getClass().getSimpleName(), observerTip);
        return baseViewModel;
    }

    protected final Observer<SourceData> observerData = this::pageData;

    protected final Observer<SourceData> observerLoading = this::pageLoading;

    protected final Observer<SourceData> observerTip = this::pageTip;
}
